﻿$(document).ready(function () {
    $("#btnAddNew").click(function () {
        if ($(this).val() == "Add new") {
            $("#addnewbox").show();
            $(this).val("Hide");
        }
        else {
            $("#addnewbox").hide();
            $(this).val("Add new");
        }
    });
});

function EditInline(row, sender) {
    $(row).DoPendingChange();
    var ischange = $(row).attr("changed");
    if ((typeof ischange == "undefined" || ischange == "") && $(sender).text() != "Done") {
        $(row).InitialEditTableCell({ emptytext: '', inline: true });
        $(row).addClass("editing");
        $(sender).text("Done");
    } else if (ischange == "true") {
        SaveInline(row, sender);
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    }
}

function SaveInline(row, sender) {
    if (typeof $(row).attr("changed") != "undefined" && $(row).attr("changed") == "true") {
        var ID = $.trim($(row).attr("receivablesreport"));
        var Receivables_Obtained = $.trim($(row).children('[code="Receivables_Obtained"]').text());
        var Receivables_Paid_Com = $.trim($(row).children('[code="Receivables_Paid_Com"]').text());
        var Receivables_Note = $.trim($(row).children('[code="Receivables_Note"]').text());
        var Receivables_Update = $.trim($(row).children('[code="Receivables_Update"]').text());
        var Receivables_PaymentMethod = $.trim($(row).children('[code="Receivables_PaymentMethod"]').text());
        
        $.ajax({
            type: 'POST',
            url: "Handlers/EditInline.ashx",
            data: {
                act: "receivablesreport",
                Receivables_Obtained: Receivables_Obtained,
                ID: ID,
                Receivables_Paid_Com: Receivables_Paid_Com,
                Receivables_Note: Receivables_Note,
                Receivables_Update: Receivables_Update,
                Receivables_PaymentMethod: Receivables_PaymentMethod,
            },
            success: function (data) {
                if (data == null) {
                    alert("Cập nhật không thành công!");
                } else if (data.Message == "sucessful") {
                    //$(row).children('td[code="LastUpdate"]').text(data.Date);
                    alert("Cập nhật thành công!");
                } else {
                    alert(data.Message);
                }

                $(row).removeAttr("changed");
                $(row).DisableEditTableCell();
                $(sender).text("Edit");
            },
            dataType: "json"
        });
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
    }
}

